版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/09/09/2013-09-09-CODE 3 Surrounded Regions/
Given a 2D board containing'X'
and'O'
, capture all regions surrounded by'X'
.
A region is captured by flipping all'O'
s into'X'
s in that surrounded region .
For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
我的代码如下:
|
|
主要思考如下:
- 这道题首先使用了dfs完成,但这种方法使用了递归所以没有通过大数据集中大数据的测试;
- 然后使用了bfs完成,但是同样的判断语句出现了很多次,也会导致运行时间过长,具体的问题代码如下:
|
|
具体是在while循环中有过多的if判断,这样导致运算次数多,若是将这些判断语句提到最前面则会减少运算次数;
- 在leetcode上貌似直接使用dfs和bfs是不行了,必须改进,比如改为非递归或者剪枝等等。